package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"strconv"
)

type LabelInfo struct {
	Id int64
	Labelid string
	Labelip string
	Labelport string
	Labeltype string
	Labeluser string
	Gender string
	Position string
	Registered string
	Unit string
	Department string
	Phonenumber string
	Idcard string
	Coordinate string
	Online string
	Electricquantity string
	Timestamp string
	Changetime string
	Alarmflag string
	Networkstatus string
	Opencamera string
	Nearelectricity string
	Badhandstatus string
	Wearingstatus string
}



//添加标签
func AddLabel(label_info *LabelInfo)(int64,error){
	db := orm.NewOrm()
	id,err := db.Insert(label_info)
	return id,err
}


//读取所有标签信息，并进行分页
func ReadLabel(labelDataChan chan string,company string,page string, size string,searchlittletype string)int{
	//fmt.Println(page)
	//fmt.Println(size)
	//使用原生查询
	var labelrows []LabelInfo
	o := orm.NewOrm()
	pageint ,_ := strconv.ParseInt(page, 10, 64)
	sizeint, _ := strconv.ParseInt(size, 10, 64)
	totalItem := 0
	if searchlittletype == "1" { //加载全部标签
		o.Raw("SELECT COUNT(*) FROM label_info where unit = ? ",company).QueryRow(&totalItem)
		fmt.Println(totalItem)
		if _, err := o.Raw("SELECT * FROM label_info WHERE unit = ?  limit ?,?", company,(pageint-1)*sizeint, sizeint).QueryRows(&labelrows); err != nil {
			fmt.Println("err")
			return 0
		}
		var  i int64= (pageint-1)*sizeint;
		for _,labelrow := range labelrows{
			i++
			id := strconv.FormatInt(i,10)
			LabelInfos :=id+ "and"+labelrow.Labelid+
				"and"+labelrow.Labeltype+
				"and"+labelrow.Labeluser+
				"and"+labelrow.Gender+
				"and"+labelrow.Position+
				"and"+labelrow.Registered+
				"and"+labelrow.Unit+
				"and"+labelrow.Department+
				"and"+labelrow.Phonenumber+
				"and"+labelrow.Idcard+
				"and"+labelrow.Coordinate+
				"and"+labelrow.Electricquantity
			fmt.Println("labelinfo:",LabelInfos)
			labelDataChan <- LabelInfos
		}
	}else if searchlittletype == "2" {//只搜索安全帽标签
		o.Raw("SELECT COUNT(*) FROM label_info where unit = ? and labeltype = '安全帽标签'",company).QueryRow(&totalItem)
		if _, err := o.Raw("SELECT * FROM label_info  where unit = ? and labeltype  = '安全帽标签' limit ?,?", company,(pageint-1)*sizeint, sizeint).QueryRows(&labelrows); err != nil {
			fmt.Println("err")
			return 0
		}
		var  i int64= (pageint-1)*sizeint;
		for _,labelrow := range labelrows{
			i++
			id := strconv.FormatInt(i,10)
			LabelInfos :=id+ "and"+labelrow.Labelid+
				"and"+labelrow.Labeltype+
				"and"+labelrow.Labeluser+
				"and"+labelrow.Gender+
			//"and"+labelrow.Position+
			   //"and"+labelrow.Registered+
			//"and"+labelrow.Unit+
			    "and"+labelrow.Department+
				"and"+labelrow.Phonenumber+
				"and"+labelrow.Idcard+
				"and"+labelrow.Coordinate+
			//"and"+labelrow.Electricquantity+
				"and"+labelrow.Networkstatus+
				"and"+labelrow.Opencamera+
				"and"+labelrow.Nearelectricity+
				"and"+labelrow.Badhandstatus+
				"and"+labelrow.Wearingstatus
			fmt.Println("labelinfo:",LabelInfos)
			labelDataChan <- LabelInfos
		}
	}else if searchlittletype == "3" { //加载在线标签
		o.Raw("SELECT COUNT(*) FROM label_info where unit = ? and online = '在线'",company).QueryRow(&totalItem)
		fmt.Println(totalItem)
		if _, err := o.Raw("SELECT * FROM label_info WHERE unit = ? and online = '在线' limit ?,?", company,(pageint-1)*sizeint, sizeint).QueryRows(&labelrows); err != nil {
			fmt.Println("err")
			return 0
		}
		var  i int64= (pageint-1)*sizeint;
		for _,labelrow := range labelrows{
			i++
			id := strconv.FormatInt(i,10)
			LabelInfos :=id+ "and"+labelrow.Labelid+
				"and"+labelrow.Labeltype+
				"and"+labelrow.Labeluser+
				"and"+labelrow.Gender+
		    	"and"+labelrow.Position+
		    	"and"+labelrow.Registered+
		    	"and"+labelrow.Unit+
		    	"and"+labelrow.Department+
				"and"+labelrow.Phonenumber+
				"and"+labelrow.Idcard+
		    	"and"+labelrow.Coordinate+
				"and"+labelrow.Networkstatus+
		    	"and"+labelrow.Electricquantity+
				"and"+labelrow.Opencamera+
				"and"+labelrow.Nearelectricity+
				"and"+labelrow.Badhandstatus+
				"and"+labelrow.Wearingstatus+
				"and"+labelrow.Online
			fmt.Println("labelinfo:",LabelInfos)
			labelDataChan <- LabelInfos
		}
	}
	return totalItem
}



//搜索标签信息
func SearchLabel(labelDataChan chan string,username string,searchlittletype string) int {
	//fmt.Println("get messge from front:",message);
    company := QueryCompanyByUsername(username)
    o := orm.NewOrm()
	//使用原生查询
	var labelrows []LabelInfo
	var totalItem int = 0
	//搜索全部标签
	if searchlittletype == "1" {
		o.Raw("SELECT COUNT(*) FROM label_info where unit = ? ",company).QueryRow(&totalItem)
		fmt.Println(totalItem)
		if _, err := o.Raw("SELECT * FROM label_info WHERE unit = ?", company).QueryRows(&labelrows); err != nil {
			fmt.Println("err")
			return 0
		}
		var  i int64= 1;
		for _,labelrow := range labelrows{
			id := strconv.FormatInt(i,10)
			LabelInfos :=id+ "and"+labelrow.Labelid+
				"and"+labelrow.Labeltype+
				"and"+labelrow.Labeluser+
				"and"+labelrow.Gender+
				"and"+labelrow.Position+
				"and"+labelrow.Registered+
				"and"+labelrow.Unit+
				"and"+labelrow.Department+
				"and"+labelrow.Phonenumber+
				"and"+labelrow.Idcard+
				"and"+labelrow.Coordinate+
				"and"+labelrow.Electricquantity
			fmt.Println("labelinfo:",LabelInfos)
			labelDataChan <- LabelInfos
			i++
		}
	}else if searchlittletype == "2" {//只搜索安全帽标签
		o.Raw("SELECT COUNT(*) FROM label_info where unit = ? and labeltype = '安全帽标签'",company).QueryRow(&totalItem)
		if _, err := o.Raw("SELECT * FROM label_info  where unit = ? and labeltype  = '安全帽标签'", company).QueryRows(&labelrows); err != nil {
			fmt.Println("err")
			return 0
		}
		var  i int64= 1;
		for _,labelrow := range labelrows{
			id := strconv.FormatInt(i,10)
			LabelInfos :=id+ "and"+labelrow.Labelid+
				"and"+labelrow.Labeltype+
				"and"+labelrow.Labeluser+
				"and"+labelrow.Gender+
				//"and"+labelrow.Position+
				//"and"+labelrow.Registered+
				//"and"+labelrow.Unit+
				"and"+labelrow.Department+
				"and"+labelrow.Phonenumber+
				"and"+labelrow.Idcard+
				"and"+labelrow.Coordinate+
				//"and"+labelrow.Electricquantity+
				"and"+labelrow.Networkstatus+
				"and"+labelrow.Opencamera+
				"and"+labelrow.Nearelectricity+
				"and"+labelrow.Badhandstatus+
				"and"+labelrow.Wearingstatus
			fmt.Println("labelinfo:",LabelInfos)
			labelDataChan <- LabelInfos
			i++
		}
	}else if searchlittletype == "3"{//搜索在线标签
		o.Raw("SELECT COUNT(*) FROM label_info where unit = ? and online = '在线'",company).QueryRow(&totalItem)
		fmt.Println(totalItem)
		if _, err := o.Raw("SELECT * FROM label_info WHERE unit = ? and online = '在线'", company).QueryRows(&labelrows); err != nil {
			fmt.Println("err")
			return 0
		}
		var  i int64= 1;
		for _,labelrow := range labelrows{
			id := strconv.FormatInt(i,10)
			LabelInfos :=id+ "and"+labelrow.Labelid+
				"and"+labelrow.Labeltype+
				"and"+labelrow.Labeluser+
				"and"+labelrow.Gender+
				"and"+labelrow.Position+
				"and"+labelrow.Registered+
				"and"+labelrow.Unit+
				"and"+labelrow.Department+
				"and"+labelrow.Phonenumber+
				"and"+labelrow.Idcard+
				"and"+labelrow.Coordinate+
				"and"+labelrow.Electricquantity+
				"and"+labelrow.Networkstatus+
				"and"+labelrow.Opencamera+
				"and"+labelrow.Nearelectricity+
				"and"+labelrow.Badhandstatus+
				"and"+labelrow.Wearingstatus+
				"and"+labelrow.Online
			fmt.Println("labelinfo:",LabelInfos)
			labelDataChan <- LabelInfos
			i++
		}


	}
	return totalItem
}

//搜索结果分页
//func ReadLabelbyPage(labelDataChan chan string,username string) {
//
//}


//根据部门区域查询标签
func Readbycondition(labelDataChan chan string,company string, department string, coordinate string,page string,size string) int {
	o := orm.NewOrm()
	var labelrows []LabelInfo
	var totalItem int = 0
	pageint ,_ := strconv.ParseInt(page, 10, 64)
	sizeint, _ := strconv.ParseInt(size, 10, 64)

	o.Raw("SELECT COUNT(*) FROM label_info where unit = ? and department = ? and coordinate = ? and online = '在线'",company,department,coordinate).QueryRow(&totalItem)

	fmt.Println(totalItem)
	if _, err := o.Raw("SELECT * FROM label_info WHERE unit = ? and department = ? and coordinate = ? and online = '在线' limit ?,? ",company,department,coordinate,(pageint-1)*sizeint, sizeint).QueryRows(&labelrows); err != nil {
		fmt.Println("err")
		return 0
	}
	var  i int64= (pageint-1)*sizeint;
	for _,labelrow := range labelrows{
		i++
		id := strconv.FormatInt(i,10)
		LabelInfos :=id+ "and"+labelrow.Labelid+
			"and"+labelrow.Labeltype+
			"and"+labelrow.Labeluser+
			"and"+labelrow.Gender+
			"and"+labelrow.Position+
			"and"+labelrow.Registered+
			"and"+labelrow.Unit+
			"and"+labelrow.Department+
			"and"+labelrow.Phonenumber+
			"and"+labelrow.Idcard+
			"and"+labelrow.Coordinate+
			"and"+labelrow.Networkstatus+
			"and"+labelrow.Electricquantity+
			"and"+labelrow.Opencamera+
			"and"+labelrow.Nearelectricity+
			"and"+labelrow.Badhandstatus+
			"and"+labelrow.Wearingstatus+
			"and"+labelrow.Online
		//fmt.Println("labelinfo:",LabelInfos)
		labelDataChan <- LabelInfos

	}
	return totalItem
}
